package com.example.other.netty.adpacket;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;

public class FirstClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        for(int i=0; i<1000; i++){
            ByteBuf byteBuf = getByteBuf(ctx);
            //操作系统底层仍然是按照字节流发送数据。因此数据到了服务端也是按照字节流读入，到了netty应用层面，重新拼装成ByteBuf
            //而这里的byteBuf与客户端按顺序发送的ByteBuf是不对等的。

            ctx.channel().writeAndFlush(byteBuf);
//        }
    }



    private ByteBuf getByteBuf(ChannelHandlerContext ctx) {
        byte[] bytes = "hello bro, ths for coming".getBytes(Charset.forName("utf-8"));
        ByteBuf byteBuf = ctx.alloc().buffer();
        byteBuf.writeBytes(bytes);
        return byteBuf;
    }
}
